#include <standard/resourcemanager.h>
#include "helper/sceneobjecthelperobject.h"
#include "terrain/terrainobject.h"
#include "terrain/terrainblock.h"
#include "sceneobjects/sceneobject.h"

CSceneObjectHelperGeometry::CSceneObjectHelperGeometry( CTerrainObject* pTerrainObject ){
	m_pTerrainObject = pTerrainObject;
}
CSceneObjectHelperGeometry::~CSceneObjectHelperGeometry(){

}
CVertexBuffer* CSceneObjectHelperGeometry::GetRenderBuffer(){
	return m_rRenderBuffer.GetEntityData();
}
CIndexBuffer* CSceneObjectHelperGeometry::GetIndexBuffer(){
	return m_rIndexBuffer.GetEntityData();
}
void CSceneObjectHelperGeometry::UpdateGeometry( list< CSceneObject* >* vpSceneObjectList ){
	vector<TV3C1T2> m_vVertex;
	vector<uint16> _vIndexData;
	color32 _cColor = 0xffffffff;

	if( vpSceneObjectList ){
		for( list< CSceneObject* >::iterator it = vpSceneObjectList->begin(); it != vpSceneObjectList->end(); it ++ ){
			CSceneObject* _pSceneObject = *it;
			float* _pPos = _pSceneObject->m_vPosition;

			float _fRadius = _pSceneObject->m_fHelperRadius;

			//if( _pSceneObject->m_eSceneObjectType == ESOT_SCENEDECORATION ){
//				SSceneDecorationConfigInfo* _pSceneDecorationConfigInfo = ((CSceneDecoration*)_pSceneObject)->m_pSceneDecortionCfgInfo;
	//			_fRadius = _pSceneDecorationConfigInfo->m_nOperatorRadius;
		//	}
		
			////CSceneObjectConfigInfo* _pSceneObjectConfigInfo = (CSceneObjectConfigInfo*)_pSceneObject->FindComponent( "CSceneObjectConfigInfo" );
			////if( _pSceneObjectConfigInfo ){
			////	_fRadius = _pSceneObjectConfigInfo->m_nOperatorRadius;
			////} else{


			////}


			float _fHalfWidth = _fRadius;
			float _fHalfHeight = _fRadius;

			TV4 _vPos[9] = {
				{ _pPos[0],					_pPos[2],					0.5f, 0.5f },
				{ _pPos[0] - _fHalfWidth,	_pPos[2] - _fHalfHeight,	0.0f, 0.0f },
				{ _pPos[0],					_pPos[2] - _fHalfHeight,	0.5f, 0.0f },
				{ _pPos[0] + _fHalfWidth,	_pPos[2] - _fHalfHeight,	1.0f, 0.0f },
				{ _pPos[0] + _fHalfWidth,	_pPos[2],					1.0f, 0.5f },
				{ _pPos[0] + _fHalfWidth,	_pPos[2] + _fHalfHeight,	1.0f, 1.0f },
				{ _pPos[0],					_pPos[2] + _fHalfHeight,	0.5f, 1.0f },
				{ _pPos[0] - _fHalfWidth,	_pPos[2] + _fHalfHeight,	0.0f, 1.0f },
				{ _pPos[0] - _fHalfWidth,	_pPos[2],					0.0f, 0.5f },
			};

			int32 _nIndex = m_vVertex.size();

			TV3C1T2 _vert;
			for( int32 i = 0; i < 9; i ++ ){
				//TV3C1T2_SETS( _vert, _vPos[i].X, m_pTerrainObject->GetSurfaceHeight( _vPos[i].X, _vPos[i].Y ) + 10.0f, _vPos[i].Y, _cColor, _vPos[i].Z, _vPos[i].W );

				TV3C1T2_SETS( _vert, _vPos[i].X, m_pTerrainObject->GetBlockGeometryHeight( _vPos[i].X, _vPos[i].Y ), _vPos[i].Y, _cColor, _vPos[i].Z, _vPos[i].W );
				m_vVertex.push_back( _vert );
			}

			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 1 );
			_vIndexData.push_back( _nIndex + 2 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 2 );
			_vIndexData.push_back( _nIndex + 3 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 3);
			_vIndexData.push_back( _nIndex + 4 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 4 );
			_vIndexData.push_back( _nIndex + 5 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 5 );
			_vIndexData.push_back( _nIndex + 6 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 6 );
			_vIndexData.push_back( _nIndex + 7 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 7 );
			_vIndexData.push_back( _nIndex + 8 );
			_vIndexData.push_back( _nIndex + 0 );
			_vIndexData.push_back( _nIndex + 8 );
			_vIndexData.push_back( _nIndex + 1 );
		}
	}

	//if( m_vVertex.size() == 0 )return;

	if( m_rRenderBuffer.IsAttached() ){
		m_rRenderBuffer->UpdateBuffer( m_vVertex.data(), m_vVertex.size() );
	} else{
		m_rRenderBuffer = NEW CVertexBuffer( EVT_VECTOR_COLOR_TEXCOORDS, m_vVertex.data(), m_vVertex.size(), True );
	}

	if( m_rIndexBuffer.IsAttached() ){
		m_rIndexBuffer->UpdateBuffer( _vIndexData.data(), _vIndexData.size() );
	} else{
		m_rIndexBuffer = NEW CIndexBuffer( _vIndexData.data(), _vIndexData.size(), True, sizeof( uint16 ) );
	}
}


///////////////////////////////////////////////////////////////////////////
CSceneObjectHelperObject::CSceneObjectHelperObject(){

}
CSceneObjectHelperObject::~CSceneObjectHelperObject(){

}
void CSceneObjectHelperObject::Init( CTerrainObject* pTerrainObject ){
	m_pTerrainObject = pTerrainObject;

	m_rSceneObjectHelperGeometry = NEW CSceneObjectHelperGeometry( pTerrainObject );
	m_rSceneObjectHelperGeometry->UpdateGeometry( NULL );

	m_rRenderItem = NEW CRenderItem( m_vWorldM );
	m_rRenderItem->m_eRenderType = 0;
	m_rRenderItem->m_bDepthWrite = True;
    m_rRenderItem->m_bDepthTest = False;
	m_rRenderItem->m_bCastShadow = False;
	m_rRenderItem->m_bReceiveShadow = False;
	m_rRenderItem->m_bFogEnable = False;
	m_rRenderItem->m_bLightEnable = False;
	m_rRenderItem->m_bLineMode = False;
	m_rRenderItem->m_eSide = BackSide;
	m_rRenderItem->m_eBlending = NormalBlending;

	m_rRenderItem->SetPrimitiveType( EPT_TRIANGLE_LIST );
	CTexture* _pTexture = RESOURCE_GETTEXTURE( "selectobject.png" );
	m_rRenderItem->CreateNormalDiffuseBatchData( _pTexture, m_rSceneObjectHelperGeometry->GetRenderBuffer(), m_rSceneObjectHelperGeometry->GetIndexBuffer() );

}
void CSceneObjectHelperObject::UpdateGeometry( list< CSceneObject* >* vpSceneObjectList ){
	m_rSceneObjectHelperGeometry->UpdateGeometry( vpSceneObjectList );
	if( vpSceneObjectList == NULL || vpSceneObjectList->empty() ){
		m_rRenderItem->SetVisible( False );
	}else{
		m_rRenderItem->SetVisible( True );
	}
}
int32 CSceneObjectHelperObject::GetRenderElementHandles( CRenderElement** vRenderElementHandle, int32 nMaxCount ){
	vRenderElementHandle[0] = m_rRenderItem->m_pRenderElement.GetEntityData();
	return 1;
}
